<<<<<<< HEAD
package com.wwx.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created by Administrator on 2017/7/9.
 */
public class RandomCodeUtils {

    public  static  void sendCode(HttpServletResponse response, HttpSession session,String sessionCodeKey){
       try{
           // 响应头信息
           response.setHeader("Pragma", "No-Cache");
           response.setHeader("Cache-Control", "no-cache");
           response.setDateHeader("Expries", 0);

           // 随机数生成类
           Random random = new Random();

           // 定义验证码的位数
           int size = 5;

           // 定义变量保存生成的验证码
           String vCode = "";
           char c;
           // 产生验证码
           for (int i = 0; i < size; i++) {
               // 产生一个26以内的随机整数
               int number = random.nextInt(26);
               // 如果生成的是偶数，则随机生成一个数字
               if (number % 2 == 0) {
                   c = (char) ('0' + (char) ((int) (Math.random() * 10)));
                   // 如果生成的是奇数，则随机生成一个字母
               } else {
                   c = (char) ((char) ((int) (Math.random() * 26)) + 'A');
               }
               vCode = vCode + c;
           }
           System.out.println(vCode);
           // 保存生成的5位验证码
           session.setAttribute(sessionCodeKey, vCode);

           // 验证码图片的生成
           // 定义图片的宽度和高度
           int width = (int) Math.ceil(size * 20);
           int height = 50;
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           // 获取图片的上下文
           Graphics gr = image.getGraphics();
           // 设定图片背景颜色
           gr.setColor(Color.WHITE);
           gr.fillRect(0, 0, width, height);
           // 设定图片边框

           // 设置字体，画验证码
           gr.setColor(randomColor());
           gr.setFont(randomFont());
           gr.drawString(vCode, 10, 33);
           // 图像生效
           gr.dispose();
           // 输出到页面
           ImageIO.write(image, "JPEG", response.getOutputStream());
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    // 生成随机的颜色
    private static Color randomColor() {
        int red = r.nextInt(150);
        int green = r.nextInt(150);
        int blue = r.nextInt(150);
        return new Color(red, green, blue);
    }

    private static String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312","篆书","隶书","燕体","草书","仿宋体"};
    private static  Random r = new Random();

    // 生成随机的字体
    private static Font randomFont() {
        int index = r.nextInt(fontNames.length);
        String fontName = fontNames[index];// 生成随机的字体名称
        int style = r.nextInt(4);
        int size = r.nextInt(3) + 24; // 生成随机字号, 24 ~ 28
        return new Font(fontName, style, size);
    }
}
=======
package com.wwx.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created by Administrator on 2017/7/9.
 */
public class RandomCodeUtils {

    public  static  void sendCode(HttpServletResponse response, HttpSession session,String sessionCodeKey){
       try{
           // 响应头信息
           response.setHeader("Pragma", "No-Cache");
           response.setHeader("Cache-Control", "no-cache");
           response.setDateHeader("Expries", 0);

           // 随机数生成类
           Random random = new Random();

           // 定义验证码的位数
           int size = 5;

           // 定义变量保存生成的验证码
           String vCode = "";
           char c;
           // 产生验证码
           for (int i = 0; i < size; i++) {
               // 产生一个26以内的随机整数
               int number = random.nextInt(26);
               // 如果生成的是偶数，则随机生成一个数字
               if (number % 2 == 0) {
                   c = (char) ('0' + (char) ((int) (Math.random() * 10)));
                   // 如果生成的是奇数，则随机生成一个字母
               } else {
                   c = (char) ((char) ((int) (Math.random() * 26)) + 'A');
               }
               vCode = vCode + c;
           }
           System.out.println(vCode);
           // 保存生成的5位验证码
           session.setAttribute(sessionCodeKey, vCode);

           // 验证码图片的生成
           // 定义图片的宽度和高度
           int width = (int) Math.ceil(size * 20);
           int height = 50;
           BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
           // 获取图片的上下文
           Graphics gr = image.getGraphics();
           // 设定图片背景颜色
           gr.setColor(Color.WHITE);
           gr.fillRect(0, 0, width, height);
           // 设定图片边框

           // 设置字体，画验证码
           gr.setColor(randomColor());
           gr.setFont(randomFont());
           gr.drawString(vCode, 10, 33);
           // 图像生效
           gr.dispose();
           // 输出到页面
           ImageIO.write(image, "JPEG", response.getOutputStream());
       }catch (Exception e){
           e.printStackTrace();
       }
    }

    // 生成随机的颜色
    private static Color randomColor() {
        int red = r.nextInt(150);
        int green = r.nextInt(150);
        int blue = r.nextInt(150);
        return new Color(red, green, blue);
    }

    private static String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312","篆书","隶书","燕体","草书","仿宋体"};
    private static  Random r = new Random();

    // 生成随机的字体
    private static Font randomFont() {
        int index = r.nextInt(fontNames.length);
        String fontName = fontNames[index];// 生成随机的字体名称
        int style = r.nextInt(4);
        int size = r.nextInt(3) + 24; // 生成随机字号, 24 ~ 28
        return new Font(fontName, style, size);
    }
}
>>>>>>> dev
